package com.topisv.tms.web.work;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.SendCountService;
import com.topisv.tms.utils.DateUtils;
import com.topisv.tms.utils.TimeUtil;
import com.topisv.tms.web.base.BaseController;
/**
 
 */
@Controller
@RequestMapping({ "/sendCount" })
public class SendCountController extends BaseController{

	@Autowired
	private SendCountService sendCountService;
	
	@Autowired
    private CustomersService customersService;
	
	/**
	 * 查询发货数量
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "query",method = RequestMethod.POST)
	public void query(@RequestParam("customerCode") String customerCode,@RequestParam("warehouseCode") String warehouseCode,
			@RequestParam("dataRange") String dataRange,HttpServletResponse response) throws Exception {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			Date fromDate=new Date();
			Date toDate=new Date();
			if(StringUtils.isNotEmpty(dataRange)){
				Map<String, Date> dataMap=DateUtils.getDateRange(dataRange);
	            fromDate=dataMap.get("fromDate");
	            toDate=dataMap.get("toDate");
			}
			String customerCodes = "";
			if(StringUtils.isBlank(customerCode)){
				ShiroUser curUser = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
				customerCodes = this.customersService.loadCustomerByAuthority(curUser.jobuuid);
			}else{
				customerCodes = customerCode;
			}
			
			List<Map<String, Object>> result = sendCountService.getSendCount(TimeUtil.getNowDateString(fromDate), TimeUtil.getNowDateString(toDate), customerCodes, warehouseCode);
			json.put("status", "1"); //查询成功
			json.put("content", result); //
		} catch (Exception e) {
			log.error("error",e);
			json.put("status", "0");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	
	/**
	 * 查询发货数量
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendCount",method = RequestMethod.POST)
	public void sendCount(@RequestParam("customerCode") String customerCode,@RequestParam("warehouseCode") String warehouseCode,
			@RequestParam("dataRange") String dataRange,HttpServletResponse response) throws Exception {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			Date fromDate=new Date();
			Date toDate=new Date();
			if(StringUtils.isNotEmpty(dataRange)){
				Map<String, Date> dataMap=DateUtils.getDateRange(dataRange);
	            fromDate=dataMap.get("fromDate");
	            toDate=dataMap.get("toDate");
			}
			List<Map<String, Object>> result = sendCountService.getSendCountByCustomerCode(TimeUtil.getNowDateString(fromDate), TimeUtil.getNowDateString(toDate), customerCode, warehouseCode);
			json.put("status", "1"); //查询成功
			json.put("content", result); //
		} catch (Exception e) {
			log.error("error",e);
			json.put("status", "0");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	/**
	 * @author zhangweize
	 * 订单各个状态数量的统计
	 * @param customerCode
	 * @param warehouseCode
	 * @param dataRange
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryOrderStatusCount",method = RequestMethod.POST)
	public void queryOrderStatusCount(@RequestParam("customerCode") String customerCode,@RequestParam("warehouseCode") String warehouseCode,
			@RequestParam("dataRange") String dataRange,HttpServletResponse response) throws Exception {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			Date fromDate=new Date();
			Date toDate=new Date();
			if(StringUtils.isNotEmpty(dataRange)){
				Map<String, Date> dataMap=DateUtils.getDateRange(dataRange);
	            fromDate=dataMap.get("fromDate");
	            toDate=dataMap.get("toDate");
			}
			List<Map<String, Object>> result = sendCountService.getOrderStatusCountByCustomerCode(TimeUtil.getNowDateString(fromDate), TimeUtil.getNowDateString(toDate), customerCode, warehouseCode);
			json.put("status", "1"); //查询成功
			json.put("content", result); //
		} catch (Exception e) {
			log.error("error",e);
			json.put("status", "0");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	/**
	 * 重复订单查询
	 * @param dataRange
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryRepeatOrder",method = RequestMethod.POST)
	public void queryRepeatOrder(@RequestParam("dataRange") String dataRange,HttpServletResponse response) throws Exception {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			Date fromDate=new Date();
			Date toDate=new Date();
			if(StringUtils.isNotEmpty(dataRange)){
				Map<String, Date> dataMap=DateUtils.getDateRange(dataRange);
	            fromDate=dataMap.get("fromDate");
	            toDate=dataMap.get("toDate");
			}
			List<Map<String, Object>> result = sendCountService.getRepeatOrderByDate(TimeUtil.getNowDateString(fromDate), TimeUtil.getNowDateString(toDate));
			json.put("status", "1"); //查询成功
			json.put("content", result); //
		} catch (Exception e) {
			log.error("error",e);
			json.put("status", "0");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	
}